package hxy.dragon.secure

import kotlin.test.*

class CryptoServiceTest {
    @Test
    fun testKeyPairGeneration() {
        val cryptoService = CryptoService()
        val keyPair = cryptoService.generateKeyPair()
        
        assertNotNull(keyPair.privateKey)
        assertNotNull(keyPair.publicKey)
        assertEquals(32, keyPair.privateKey.size)
        assertEquals(32, keyPair.publicKey.size)
    }
    
    @Test
    fun testBase64EncodingAndDecoding() {
        val originalBytes = byteArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
        val encodedString = originalBytes.encodeBase64()
        val decodedBytes = encodedString.decodeBase64()
        
        assertContentEquals(originalBytes, decodedBytes)
    }
    
    @Test
    fun testParseSshEd25519PublicKey() {
        // 测试提供的SSH ED25519公钥
        val sshEd25519Key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPSEOTVqftlcxz24oP8erbsq/XqF9cOSjJu9zH4BCJjC aohanhongzhi@qq.com"
        
        val parsedKey = parseSshEd25519PublicKey(sshEd25519Key)
        
        // 验证解析出的公钥长度为32字节
        assertEquals(32, parsedKey.size)
    }
    
    @Test
    fun testConvertEd25519ToX25519Key() {
        // 生成一个测试密钥
        val testKey = ByteArray(32) { i -> i.toByte() }
        
        // 转换密钥
        val convertedKey = convertEd25519ToX25519Key(testKey)
        
        // 验证转换后的密钥长度仍为32字节
        assertEquals(32, convertedKey.size)
        
        // 验证相同输入产生相同输出（幂等性）
        val convertedKey2 = convertEd25519ToX25519Key(testKey)
        assertContentEquals(convertedKey, convertedKey2)
    }
    
    @Test
    fun testOpenSSHPrivateKeyParsing() {
        // 测试OpenSSH私钥解析
        val openSSHPrivateKey = """-----BEGIN OPENSSH PRIVATE KEY-----
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
QyNTUxOQAAACD0hDk1an7ZXMc9uKD/Hq27Kv16hfXDkoybvcx+AQiYwgAAAJi5zU0Tuc1N
EwAAAAtzc2gtZWQyNTUxOQAAACD0hDk1an7ZXMc9uKD/Hq27Kv16hfXDkoybvcx+AQiYwg
AAAED4NfJ/T2amYrgStidoIsphfaFc30lGax/C4RA6YC1be/SEOTVqftlcxz24oP8erbsq
/XqF9cOSjJu9zH4BCJjCAAAAE2FvaGFuaG9uZ3poaUBxcS5jb20BAg==
-----END OPENSSH PRIVATE KEY-----"""
        
        val parsedKey = parseOpenSSHPrivateKey(openSSHPrivateKey)
        
        // 验证解析出的私钥长度为32字节
        assertEquals(32, parsedKey.size)
    }
    
    @Test
    fun testEncryptionAndDecryption() {
        val cryptoService = CryptoService()
        
        // 使用固定的密钥对进行测试
        val privateKey = ByteArray(32) { i -> i.toByte() }
        val publicKey = ByteArray(32) { i -> (i * 2).toByte() }
        
        // 计算共享密钥
        val sharedSecret1 = cryptoService.computeSharedSecret(privateKey, publicKey)
        val sharedSecret2 = cryptoService.computeSharedSecret(privateKey, publicKey)
        
        // 验证两次计算的共享密钥相同
        assertContentEquals(sharedSecret1, sharedSecret2)
        
        // 验证共享密钥长度为32字节
        assertEquals(32, sharedSecret1.size)
    }
    
    @Test
    fun testDeriveKey() {
        val secret = ByteArray(32) { i -> i.toByte() }
        val info = "test"
        val length = 32
        
        val key1 = deriveKey(secret, info, length)
        val key2 = deriveKey(secret, info, length)
        
        // 验证派生的密钥长度正确
        assertEquals(length, key1.size)
        
        // 验证相同的输入产生相同的输出
        assertContentEquals(key1, key2)
    }
}